package com.cn.qtms.service.impl;//类注释设置模板

import com.cn.qtms.base.dao.BaseDao;
import com.cn.qtms.base.service.impl.BaseServiceImpl;
import com.cn.qtms.common.result.ErrorCode;
import com.cn.qtms.dao.TmSysUserDao;
import com.cn.qtms.entity.TmSysRole;
import com.cn.qtms.entity.TmSysUser;
import com.cn.qtms.exception.AppException;
import com.cn.qtms.service.TmSysUserService;
import com.cn.qtms.entity.vo.sys.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Name:TmSysUserServiceImpl
 * @Description:
 * @Author: zhangyang
 * @Date: 2019-11-11 13:19
 **/
@Service
public class TmSysUserServiceImpl extends BaseServiceImpl<TmSysUser, Long> implements TmSysUserService {
    @Autowired
    private TmSysUserDao dao;
    @Autowired
    public BCryptPasswordEncoder bCryptPasswordEncoder;

    @Override
    public BaseDao<TmSysUser, Long> getDao() {
        return dao;
    }

    @Override
    public User findByUserName(String username) {
        User user = new User();
        final TmSysUser sysUser = dao.findByUserName(username);
        BeanUtils.copyProperties(sysUser, user);
        final Set<TmSysRole> tmSysRoles = sysUser.getTmSysRoles();
        //删除掉没有启用的角色
        tmSysRoles.removeIf(value -> {
            return value.getIsUsed() == null || value.getIsUsed() == false || value.getIsDelete() != false;
        });
        if (tmSysRoles == null || tmSysRoles.size() == 0) {
            throw new AppException(ErrorCode.ACCESS_DENIED.getMessage());
        }
        List<String> roles = tmSysRoles.stream().map(TmSysRole::getRoleCode).collect(Collectors.toList());
        user.setRoles(roles);
        return user;
    }

    @Override
    public void afterSearch(TmSysUser tmSysUser) {
        final Set<TmSysRole> tmSysRoles = tmSysUser.getTmSysRoles();
        List<String> roles = tmSysRoles.stream().map(TmSysRole::getRoleCode).collect(Collectors.toList());
        tmSysUser.setRoles(roles);
        super.afterSearch(tmSysUser);
    }

    @Override
    public User findByPhone(String phone) {
        User user = new User();
        final TmSysUser sysUser = dao.findByPhone(phone);
        BeanUtils.copyProperties(sysUser, user);
        final Set<TmSysRole> tmSysRoles = sysUser.getTmSysRoles();
        //删除掉没有启用的角色
        tmSysRoles.removeIf(value -> {
            return value.getIsUsed() == null || value.getIsUsed() == false || value.getIsDelete() != false;
        });
        if (tmSysRoles == null || tmSysRoles.size() == 0) {
            throw new AppException(ErrorCode.ACCESS_DENIED.getMessage());
        }
        List<String> roles = tmSysRoles.stream().map(TmSysRole::getRoleCode).collect(Collectors.toList());
        user.setRoles(roles);
        return user;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePwd(String username, String password) {
        String pwd = bCryptPasswordEncoder.encode(password);
        dao.updatePwd(username, pwd);
    }
}
